using System;
using System.IO;

using System.Collections.Generic;
using System.Xml;
using System.Text;
using System.Data;
using Microsoft.SharePoint;
using CKS.EnhancedContentQueryWebPart;
using System.Globalization;
using System.Reflection;

namespace CKS.EnhancedContentQueryWebPart.Wrappers
{
    public class ListWrap
    {
        #region properties
        private SPList list;
        public string ScriptContextID;
        public int ContextID;
        public Guid ListID;
        public string ListRootFolder;
        public string ListTitle;
        public string SiteTitle;
        public bool ForceCheckout;

        #region List Base Type
        private int listBaseType;
        public int ListBaseType
        {
            get
            {
                return listBaseType;
            }
        }
        #endregion

        #region List Template
        private int listTemplate;
        public int ListTemplate
        {
            get
            {
                return listTemplate;
            }
        }
        public string SendToLocationName
        {
            get
            {
                return this.list.SendToLocationName;
            }
        }
        #endregion

        #region List Edit Form Url
        private string listEditFormUrl;
        public string ListEditFormUrl { get { return listEditFormUrl; } }
        #endregion

        #region List Display Form Url
        private string listDisplayFormUrl;
        public string ListDisplayFormUrl { get { return listDisplayFormUrl; } }
        #endregion
        public string ContextScriptBlock
        {
            get
            {
                if (list != null)
                {
                    try
                    {
                        string listContextScript = ScriptHelper.UnpackScript("ListContextScript.js");
                        return string.Format(listContextScript,
                                this.ListBaseType,
                                this.ListTemplate,
                                "{" + this.ListID.ToString() + "}",
                                this.ListRootFolder,
                                "/_vti_bin/owssvr.dll?CS=65001",
                                list.ParentWeb.Url,
                                 "/_layouts/images/",
                                 "/",
                                 this.ListDisplayFormUrl,
                                 this.ListEditFormUrl,
                                 0,
                                 this.ContextID,
                                 this.SendToLocationName,
                                 this.ForceCheckout.ToString().ToLower(),
                                 this.ScriptContextID);
                    }
                    catch { return ""; }
                }
                else
                {
                    return "";
                }
            }
        }
        #endregion

        #region CTOR
        public ListWrap(ListItemWrap item)
        {
            if (item.ListItem != null)
            {
                list = item.ListItem.ParentList;
                this.ListID = item.ListID;
                if (item.ListItem != null)
                {
                    this.ListRootFolder = item.ListItem.ParentList.RootFolder.Url;
                    this.ListTitle = item.ListItem.ParentList.Title;
                    this.SiteTitle = item.SiteTitle;
                    listBaseType = item.ListItem.ParentList.BaseType.GetHashCode();
                    listTemplate = item.ListItem.ParentList.BaseTemplate.GetHashCode();
                    listEditFormUrl = item.ListItem.ParentList.ParentWeb.Url + "/" + item.ListItem.ParentList.Forms[PAGETYPE.PAGE_EDITFORM].Url;
                    listDisplayFormUrl = item.ListItem.ParentList.ParentWeb.Url + "/" + item.ListItem.ParentList.Forms[PAGETYPE.PAGE_DISPLAYFORM].Url;
                    item.ListItem.ParentList.ParentWeb.Dispose();
                }
            }
            else
            {
                list = null;
            }
        }
        #endregion
    }
    /// <summary>
    /// Utility class for extracting embedded scripts.
    /// taken from http://haacked.com/archive/2005/04/29/using-embedded-resources-for-client-scripts.aspx
    /// </summary>
    /// <remarks>
    /// Uses a naming convention. All scripts should be placed 
    /// in the Resources\Scripts folder. The scriptName is just 
    /// the filename of the script.
    /// </remarks>
    internal static class ScriptHelper
    {
        /// <summary>
        /// Returns the contents of the embedded script as
        /// a stringwrapped with the start / end script tags.
        /// </summary>
        /// <param name="scriptName">FileName of the script.</param>
        /// <returns>Contents of the script.</returns>
        public static string UnpackScript(string scriptName)
        {
            string language = "javascript";
            string extension = Path.GetExtension(scriptName);

            if (0 == string.Compare(extension, ".vbs", true
              , CultureInfo.InvariantCulture))
            {
                language = "vbscript";
            }

            return UnpackScript(scriptName, language);
        }

        public static string UnpackScript(string scriptName, string scriptLanguage)
        {
            return "<script language=\"Javascript\">"
              + Environment.NewLine
              + UnpackEmbeddedResourceToString("CKS.EnhancedContentQueryWebPart.ClientScripts." + scriptName)
              + Environment.NewLine
              + "</script>";
        }

        // Unpacks the embedded resource to string.
        static string UnpackEmbeddedResourceToString(string resourceName)
        {
            Assembly executingAssembly = Assembly.GetExecutingAssembly();
            Stream resourceStream =
                executingAssembly.GetManifestResourceStream(resourceName);
            using (StreamReader reader = new StreamReader(resourceStream, Encoding.ASCII))
            {
                return reader.ReadToEnd();
            }
        }
    }
}
